/**
 * 
 */
package dms.presentation.action.student;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dms.dto.RepairDTO;
import dms.model.form.Repair;
import dms.model.role.Student;
import dms.model.role.wrapper.StudentWrapper;
import dms.model.util.PageIterator;
import dms.service.RepairQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.DateFormatUtil;
import dms.util.PageUtil;

/**
 * @author 李健
 *
 */
public class RepairAction extends StudentBaseAction {
	private final static Log log = LogFactory.getLog(RepairAction.class);
	
	public ActionForward newRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Student student = this.getStudentWrapper().getStudent();
		request.setAttribute("student", student);
		return mapping.findForward(Constant.SUCCESS);
		
	}
	
	public ActionForward addRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RepairDTO repairDTO = (RepairDTO) form;
		StudentWrapper studentWrapper = this.getStudentWrapper();
		JSONObject jsonObject = null;
		try {
			Repair repair = studentWrapper.addRepair(repairDTO);
			if (repair != null) {
				jsonObject = this.makeJSONObject(true, "添加成功！");
			} else {
				jsonObject = this.makeJSONObject(false, "添加失败!");
			}
		} catch (Exception e) {
			jsonObject = this.makeJSONObject(false, "添加失败,请稍候再试!");
		}

		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		
		return null;
	}
	
	public ActionForward repairList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RepairDTO repairDTO = (RepairDTO) form;
		int[] params = null;
		String type = repairDTO.getType();
		Date fromDate = DateFormatUtil.parseDate(repairDTO.getFromDate());
		Date toDate = DateFormatUtil.parseDate(repairDTO.getToDate());
		Boolean processed = repairDTO.getProcessed();
		Boolean applied = repairDTO.getApplied();
		PageIterator<Repair> repairs = null;
		RepairQueryService repairQueryService = (RepairQueryService) ContextUtil.getContext().getBean("repairQueryService");
		Student student = this.getStudentWrapper().getStudent();
		try {
			params = PageUtil.parsePageParameter(request);
			int start = params[0];
			int count = params[1];
			if (type != null && type.length() != 0) {
				if(type.equalsIgnoreCase("student")) {
					repairs = null;
				} else if (type.equalsIgnoreCase("dormitory")) {
					repairs = repairQueryService.findByDormitory(start, count, 
								fromDate, toDate, 
								student.getDormitory(), 
								processed, applied);
				}
			} else {
				repairs = repairQueryService.findByDormitory(start, count, 
							fromDate, toDate, 
							student.getDormitory(), 
							processed, applied);
			}
 			request.setAttribute("repairs", repairs);
 			request.setAttribute("student", student);
		} catch (Exception e) {
			return mapping.findForward(Constant.FAILURE);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward deleteRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentWrapper studentWrapper = this.getStudentWrapper();
		String[] ids = request.getParameterValues("repairIds");
		List<Long> repairIds = new ArrayList<Long>();
		JSONObject jsonObject = null;
		try {
			for (String id : ids) {
				Long repairId = Long.valueOf(id);
				repairIds.add(repairId);
			}
		} catch (Exception e) {
			jsonObject = this.makeJSONObject(false, "维修单ID错误");
		}
		
		try {
			studentWrapper.deleteRepairs(repairIds);
			jsonObject = this.makeJSONObject(true, "删除成功！");
		} catch (Exception e) {
			jsonObject = this.makeJSONObject(false, "删除失败...");
		}
		
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		log.info(jsonObject.toString());
		return null;
	}
	
	public ActionForward updateRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RepairDTO repairDTO = (RepairDTO) form;
		StudentWrapper studentWrapper = this.getStudentWrapper();
		JSONObject jsonObject = null;
		try {
			studentWrapper.updateRepair(repairDTO);
			jsonObject = this.makeJSONObject(true, "更新成功!");
		} catch (Exception e) {
			jsonObject = this.makeJSONObject(false, "更新失败...");
		}
		
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		log.info(jsonObject.toString());
		return null;
		
	}
	
/*	public ActionForward findRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RepairQueryService repairQueryService = (RepairQueryService) ContextUtil.getContext().getBean("repairQueryService");
		Long repairId = null;
		Repair repair = null;
		JSONObject jsonObject = null;
		try {
			repairId = Long.valueOf(request.getParameter("repairId"));
			repair = repairQueryService.findRepairById(repairId);
		} catch (Exception e) {
			throw e;
		}
		
	}*/
}
